package apps.reading.spider;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;

import apps.reading.ReadingSpider;
import apps.reading.WebSourceParser;
import apps.reading.db.WebSourceBean;
import core.tool.common.DateHelper;
import core.tool.common.StringHelper;
import core.tool.htmlparser.HtmlparserUtil;
@ReadingSpider(sourceType="0", type="0", url = "http://news.cnblogs.com")
public class CnBologsNews implements WebSourceParser {
	
	
	private WebSourceBean getWebSourceBeanOfStr(String result,String url) {
		WebSourceBean webSourceBean=new WebSourceBean();
		try {
			Parser parser = new Parser();
			parser.setInputHTML(result);
			NodeFilter filter = new TagNameFilter("A");
			NodeList nodelist = parser.parse(filter);
			for (int i = 0; i < nodelist.size(); i++) {
				Node node = nodelist.elementAt(i);
				LinkTag link = (LinkTag) nodelist.elementAt(i);
				if (i == 0) {
					webSourceBean.setContentURL(url+(link.getAttribute("href")));
					webSourceBean.setTitle((link.getStringText()));
				}
				if (i == 1) {
					webSourceBean.setRemark(node.getNextSibling().toHtml());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return webSourceBean;
	}
	
	
	public List<WebSourceBean> getWebSourceTitleList(String url) {
		List<WebSourceBean> webSourceList=new ArrayList<WebSourceBean>();
        Node[] nodes = HtmlparserUtil.getNodeList(url, "div", "class", "content");
        String line = "";
        for (int i = 0; i < nodes.length; i++) {
            Node node = nodes[i];
  		  	String result=node.toHtml();
            webSourceList.add(getWebSourceBeanOfStr(result,url));
        }		
		return webSourceList;
	}

	public WebSourceBean getContent(WebSourceBean webSourceBean) {
		String content=HtmlparserUtil.getNodeStr(webSourceBean.getContentURL(), "div", "id", "news_body",true);
		webSourceBean.setContent(content);
		String timeStr=HtmlparserUtil.getNodeStr(webSourceBean.getContentURL(), "span", "class", "time",false);
		String ss=timeStr.substring(4);
		webSourceBean.setPublicDate(DateHelper.getDate(ss));
		return webSourceBean;
	}
	

}
